package com.peng.leetcode.tree;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

/**
 * InorderTraversal
 * 94. 二叉树的中序遍历
 * https://leetcode.cn/problems/binary-tree-inorder-traversal/
 * <p>
 * Created on 2022/11/29
 *
 * @author lupeng
 */
public class InorderTraversal {

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        ArrayDeque<TreeNode> q = new ArrayDeque<>();
        while (root != null || !q.isEmpty()) {
            while (root != null) {
                q.offer(root);
                root = root.left;
            }
            root = q.pollLast();
            // deal
            res.add(root.val);
            root = root.right;
        }
        return res;
    }
}
